package com.shd.edanpei.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.Toast;

import com.mob.MobSDK;
import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.bean.UrlModel;
import com.shd.edanpei.db.DaoManager;
import com.shd.edanpei.util.ScreenUtils;

import java.util.HashMap;

import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.sharesdk.alipay.friends.Alipay;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

public class ShareDialogActivity extends Activity {

    private String mType;
    private UrlModel mUrlModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setFinishOnTouchOutside(true);
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.width = ScreenUtils.getScreenWidth(this);
        getWindow().setAttributes(attributes);
        getWindow().setGravity(Gravity.BOTTOM);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog_share);
        ButterKnife.bind(this);

        Bundle extras = getIntent().getExtras();
        mType = extras.getString(Constant.TYPE);
        mUrlModel = extras.getParcelable(Constant.DATA);
    }

    @OnClick(R.id.tv_share_wechatmoments)
    void shareWechatMoments() {
        showShare(WechatMoments.NAME);
    }

    @OnClick(R.id.tv_share_wechat)
    void shareWechat() {
        showShare(Wechat.NAME);
    }

    @OnClick(R.id.tv_share_qq)
    void shareQQ() {
        showShare(QQ.NAME);


    }



    class MyPlatformActionListener implements PlatformActionListener {
        @Override
        public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(MobSDK.getContext(), "Share Complete", Toast.LENGTH_SHORT).show();
                }
            });
        }

        @Override
        public void onError(Platform platform, int i, Throwable throwable) {
            throwable.printStackTrace();
            final String error = throwable.toString();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(MobSDK.getContext(), "Share Failure" + error, Toast.LENGTH_SHORT).show();
                }
            });
        }

        @Override
        public void onCancel(Platform platform, int i) {
            Toast.makeText(MobSDK.getContext(), "Cancel Share", Toast.LENGTH_SHORT).show();
        }
    }


    private void showShare(String platform) {

        OnekeyShare oks = new OnekeyShare();
        oks.disableSSOWhenAuthorize();
        //图片url
        if (mType.equals(Constant.SHARE_URL)) {
            oks.setImageUrl(mUrlModel.getUrlPicturePath());
            if (platform.equals(QQ.NAME) || platform.equals(Alipay.NAME)) {
                oks.setTitleUrl(mUrlModel.getUrlShare() + "?id=" + DaoManager.getInstance().getUserBean().getId());
            } else {
                oks.setUrl(mUrlModel.getUrlShare() + "?id=" + DaoManager.getInstance().getUserBean().getId());
            }
        } else if (mType.equals(Constant.SHARE_PIC)) {
            oks.setImagePath(mUrlModel.getPicFile());
        }
        oks.setTitle(mUrlModel.getTitle());
        oks.setText(mUrlModel.getContent());
        oks.setPlatform(platform);
        oks.show(this);
    }


    @OnClick(R.id.tv_share_cancel)
    void close() {
        finish();
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.trans_bottom_in, R.anim.trans_bottom_out);
    }

    public static void start(BaseActivity activity, Bundle bundle) {
        activity.startActivity(ShareDialogActivity.class, bundle);
        activity.overridePendingTransition(R.anim.trans_bottom_in, R.anim.trans_bottom_out);
    }

    public static void start(FragmentActivity activity, Bundle bundle) {
        activity.startActivity(new Intent(activity, ShareDialogActivity.class), bundle);
        activity.overridePendingTransition(R.anim.trans_bottom_in, R.anim.trans_bottom_out);
    }
}
